// pages/duobao/index/index.js
import S_request from '../../../utils/requestService.js';
import CONFIG from '../../../config.js';
import * as utils from '../../../utils/util.js';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowkj: true,
    isShowLogin: false,
    gold_num: 0,
    fuliStyle: '',
    mineStyle: '',
    dbList: [],
    myInfo: {},
    quanList: [{
      id: 1,
      gold: 1000,
      num: 1
    }, {
      id: 2,
      gold: 2700,
      num: 3
    }, {
      id: 3,
      gold: 4000,
      num: 5
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //1,用户授权
    this.isAuthUserinfo();
    this.getDuobaoList();
    this.getMyDBInfo();
    this.setData({
      fuliStyle: 'font-size: 40rpx;color: #dc362b;',
      mineStyle: 'font-size: 30rpx;color: #333;',
      fuliBody: true,
      mineBody: false
    })
  },
  //我的兑换
  excTicktes: function(e) {
    let id = e.currentTarget.dataset.id;
    for (let i = 0; i < this.data.quanList.length; i++) {
      let item = this.data.quanList[i];
      if (item.id == id) {
        S_request.duobao.excTicket(item.num, item.gold, (result) => {
          this.getMyDBInfo();
        })
      }
    }
  },
  getDuobaoList: function() {
    S_request.duobao.getDuobaoList((result) => {
      for (let i = 0; i < result.lists.length; i++) {
        let item = result.lists[i];
        item.open_time = utils.formatTime(item.open_time, 2);
        if (item.status == 0) { //未开奖
          if (item.join == 0) { //未参与抽奖，显示立即参与
            item.btnTitle = '立即参与'
            item.btnStyle = 'border:0; background-color:#ffc107;'
          } else {
            item.btnTitle = '等待开奖'
            item.btnStyle = 'border:0; background-color:#e42c2c;'
          }
        } else { //1：已开奖
          item.btnTitle = '已开奖'
          item.btnStyle = 'border:0; background-color:#9a9898;'
        }
      }
      this.setData({
        dbList: result.lists
      })
    })
  },
  getMyDBInfo: function() {
    S_request.duobao.myDBInfo((result) => {
      console.log("result111==" + JSON.stringify(result))
      this.setData({
        myInfo: result
      })
    })
  },
  cjClick: function() {
    wx.navigateTo({
      url: '../cjRec/cjRec',
    })
  },
  zjClick: function() {
    wx.navigateTo({
      url: '../zjRec/zjRec',
    })
  },
  clickFuli: function() {
    this.setData({
      fuliStyle: 'font-size: 40rpx;color: #dc362b;',
      mineStyle: 'font-size: 30rpx;color: #333;',
      fuliBody: true,
      mineBody: false
    })
  },
  clickMine: function() {
    this.setData({
      fuliStyle: 'font-size: 30rpx;color: #333;',
      mineStyle: 'font-size: 40rpx;color: #dc362b;',
      fuliBody: false,
      mineBody: true
    })
  },
  patiCJ: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?id=' + id,
    })

    /* 今日福利： 立即参与，等待开奖，已开奖， 点击直接进入详情页
    for (let i = 0; i < this.data.dbList.length; i++) {
      let item = this.data.dbList[i];
      if (item.id == id) {
        if (item.join == 0) { //未参与抽奖，显示立即参与
          console.log("aaaa==" + item.money + "dss==" + this.data.myInfo.tickets)
          if (parseInt(item.money) > parseInt(this.data.myInfo.tickets)) {
            wx.showToast({
              title: '抽奖券不足',
              icon: 'none',
              duration: 2000
            })
          } else {
            S_request.duobao.joinDB(id, (result) => {
              wx.showModal({
                title: '提示',
                content: '参与成功',
                showCancel: false
              })
              this.getDuobaoList();
            })
          }
        } else {
          wx.navigateTo({
            url: '../detail/detail?id=' + id,
          })
        }
        break;
      }

    }
    */
  },
  toDetail: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    wx.showShareMenu({
      withShareTicket: true,
      success: function() {}
    });
    let obj = { 'pagepath': CONFIG.SHARE.pathDuobao}
    S_request.index.recordShare("-6", obj);
    return {
      title: CONFIG.SHARE.titleDuobao,
      path: CONFIG.SHARE.pathDuobao,
      imageUrl: CONFIG.SHARE.imagDuobao,
      success: res => { //新版不走回调
        console.log("分享成功")
      }
    }
  },
  isAuthUserinfo: function() {
    let self = this;
    // 获取用户信息
    wx.getSetting({
      success: res => {
        var authSetting = res.authSetting;
        if (authSetting['scope.userInfo'] == true) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          app.globalData.isAuthorize = true;
          this.getUserInfo();
          this.modalLoginRightAc();
        } else if (authSetting['scope.userInfo'] == false) {
          // 用户已拒绝授权，再调用相关 API 或者 wx.authorize 会失败，需要引导用户到设置页面打开授权开关
          app.globalData.isAuthorize = false;
          this.showLoginModal();
        } else {
          // 未询问过用户授权，调用相关 API 或者 wx.authorize 会弹窗询问用户
          app.globalData.isAuthorize = false;
          this.showLoginModal();
        }
      }
    })
  },
  getUserInfo: function() {
    wx.getUserInfo({
      success: res => {
        // 可以将 res 发送给后台解码出 unionId
        app.globalData.userInfo = res.userInfo;
        this.setData({
          userinfo: app.globalData.userInfo,
        });
        S_request.index.updateUserInfo(res.userInfo, (result) => {
          // console.log("更新与用户信息成功====" + JSON.stringify(result));
          app.globalData.goldnum = result.goldcoin;
          this.setData({
            gold_num: utils.parseGoldNum(result.goldcoin)
          })
        })
      }
    })
  },
  showLoginModal: function() {
    this.setData({
      modalDealIdx: 1,
      isShowLogin: true
    })
  },
  modalLoginRightAc: function() {
    this.setData({
      modalDealIdx: 0,
      isShowLogin: false
    })
  },
  formSubmit: function(e) {
    // formId = e.detail.formId;
    console.log('form发生了submit事件，携带数据为：', e.detail.value + ", " + e.detail.formId)
  }
})